<?php

namespace App\Modules\Admin\Services;

use App\Modules\Admin\Repositories\Contracts\ActivityRepository;

class ActivityService
{
    private $activityRepository;

    public function __construct(ActivityRepository $activityRepository)
    {
        $this->activityRepository = $activityRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            $result = $this->activityRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function getAllActivityName()
    {
        try {
            $result = $this->activityRepository->getAllActivityName();
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->activityRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $activity = $this->activityRepository->store($param);
            return $activity;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $activity = $this->activityRepository->update($param, $id);
            return $activity;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $activity = $this->activityRepository->delete($id);

            $result = [];
            if ($activity) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function setStatus($param)
    {
        try {
            $data = [
                'status' => $param['status'],
            ];

            $activity = $this->activityRepository->update($data, $param['id']);
            return $activity;
        } catch (\Throwable $th) {
            throw $th;
        }

    }
}
